<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	
	protected $gzip;
	/**
	 * Initialization view
	 */
	protected function _initView() {
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		if (null === $viewRenderer->view) {
			$viewRenderer->initView();
		}
		$view = $viewRenderer->view;
		
		$settings = $this->getOptions();
		
		$view->headTitle($settings['web']['sitename'])->setSeparator(' : ');
		
		$view->doctype ( 'XHTML1_TRANSITIONAL' );
		
		//Init view helpers
		$view->addHelperPath(APPLICATION_PATH . "/views/helpers", "Application_View_Helper_");
		
		$modules = Nimblecms_Module_Loader::getInstance()->getModuleNames();
		
		foreach($modules as $module) {
			
			$view->addHelperPath(APPLICATION_PATH . "/modules/" . $module . "/views/helpers", ucfirst($module) . "_" . "View_Helper_");
	
		}
		
		$view->addHelperPath(APPLICATION_PATH . "/../library/ZFEngine/View/Helper/", "ZFEngine_View_Helper_");
		
		//Устанавливаем константы макета
		$view->STATIC_SERVER = $settings['web']['static_server'];
		$view->SITENAME = $settings['web']['sitename'];
		
                //устанавливаем базовый url
                $view->getHelper('BaseUrl')->setBaseUrl($settings['web']['site']);
		//$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		//$viewRenderer->setView($view);
		
		return $view;
	}
	
	protected function _initZFDebug() {		
		
	}
        
       
	
	
	/**
	 * 
	 * Initialization autoload namespaces
	 */
	protected function _initAutoload() {
		$autoLoader = Zend_Loader_Autoloader::getInstance ();
		
		$modules = Nimblecms_Module_Loader::getInstance()->getModuleNames();
               
                
		foreach($modules as $module) {
			$resourceLoader = new Zend_Loader_Autoloader_Resource ( array (
			'basePath' => APPLICATION_PATH . DS . "modules" . DS . $module, 
			'namespace' => ucfirst($module) . "_",
			'resourceTypes' => array (
					'form' => array(
						'path' => "forms/",
						'namespace' => 'Form_'
					),
					'model' => array(
						'path' => "models/",
						'namespace' => 'Model_'
					),
					'widgets' => array(
						'path' => "widgets/",
						'namespace' => 'Widgets_',
					),
					'service' => array(
						'path' => "service/",
						'namespace' => "Service_",
					),
				)
			));
			
		}
		
		
		return $autoLoader;
	}
	
	/**
	 * Initialization routes
	 */
	protected function _initRoutes() {
		$this->bootstrap('FrontController');
		$front = $this->getResource('FrontController');
		$routes = Nimblecms_Module_Loader::getInstance()->getRoutes();
		
		$front->setRouter($routes);
		
		$front->getRouter()->removeDefaultRoutes();
	}
	
	
	/**
	 * Initialization plugins
	 */
	protected function _initPlugins() {
            
            $this->bootstrap('FrontController');
            $front = $this->getResource('FrontController');
            $front->registerPlugin(new Nimblecms_Controller_Plugin_LocalizationRoute());
            $front->registerPlugin(new Nimblecms_Controller_Plugin_Auth());
            $front->registerPlugin(new Nimblecms_Controller_Plugin_Acl());
            //$front->registerPlugin(new Nimblecms_Controller_Plugin_Htmlcompressor());
            $front->registerPlugin(new Nimblecms_Controller_Plugin_Navigation());
			
            //Получаем активные плагины
            $activatedPlugins = Nimblecms_Module_Loader::getInstance()->getActivePlugins();
           
            foreach($activatedPlugins as $plugin) {
                    $pluginClass = 'Application_Plugin_' . $plugin . '_Plugin';

                    if(class_exists($pluginClass)) {
                            $pluginInstance = new $pluginClass();
                            if($pluginInstance instanceof Nimblecms_Controller_Plugin) {
                                    $front->registerPlugin($pluginInstance);
                            }
                    } else {
                            throw new Zend_Exception('Plugin ' . $pluginClass . ' not found');
                    }
             }
	}
	/*
	public function run()
    {
        $this->getResource('FrontController');
        $frontController = Zend_Controller_Front::getInstance();

        $frontController->returnResponse(true);
        
        $default = $frontController->getDefaultModule();
        if (null === $frontController->getControllerDirectory($default)) {
            throw new Zend_Application_Bootstrap_Exception(
                'No default controller directory registered with front controller'
            );
        }

        $frontController->setParam('bootstrap', $this);
        $response = $frontController->dispatch();
        if ($this->gzip) {
            $response->setHeader('Content-encoding', 'gzip');
            $response->setBody(gzencode($response->getBody(), 9));
            $response->sendResponse();
            $frontController->returnResponse(false);
        }
        if ($frontController->returnResponse()) {
             $response->sendResponse();
        }
    }
    */
	/*
	protected function _initGzip()
    {
        if (extension_loaded( 'zlib' ) 
            && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) 
            && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false 
                || strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate' ) !== false)) {
            if (ini_get('zlib.output_compression')) {
                $this->gzip = true;
            } else {
                ob_start('ob_gzhandler', 9);
            }
        }
    }
	*/
	/**
	* Init. admin menu
	*/
	protected function _initNavigation() {
		$this->bootstrapView();
		$view = $this->getResource('view');
		
		$container = new Zend_Navigation(
			array(
				array(
					'label' => _('Dashboard'),
					'route' => 'default_dashboard_index',
					'module' => 'default',
					'controller' => 'dashboard',
					'action' => 'index',
				),
				array(
					'label' => _('News'),
					'route' => 'news_news_index',
					'module' => 'news',
					'controller' => 'news',
					'action' => 'index'
				),
				array(
					'label' => _('Photogallery'),
					'route' => 'gallery_photo_index',
					'module' => 'gallery',
					'controller' => 'photo',
					'action' => 'index'
				),
				array(
					'label' => _('Navigation'),
					'route' => 'navigation_menu_index',
					'module' => 'navigation',
					'controller' => 'menu',
					'action' => 'index',
					'pages' => array(
						array(
							'label' => _('Menu links'),
							'route' => 'navigation_menuitem_index',
							'module' => 'navigation',
							'controller' => 'menuitem',
							'action' => 'index'
						)
					)
				),
				array(
					'label' => _('Pages'),
					'route' => 'pages_page_index',
					'module' => 'pages',
					'controller' => 'page',
					'action' => 'index'
				),
				array(
					'label' => _('Contacts'),
					'route' => 'pages_contacts_index',
					'module' => 'pages',
					'controller' => 'contacts',
					'action' => 'index'
				),
				array(
					'label' => _('Feedback Messages'),
					'route' => 'default_feedback_list',
					'module' => 'default',
					'controller' => 'feedback',
					'action' => 'list'
				),
				array(
					'label' => _('Site settings'),
					'route' => 'default_dashboard_settings',
					'module' => 'default',
					'controller' => 'dashboard',
					'action' => 'settings'
				),
				array(
					'label' => _('Update search index'),
					'route' => 'default_dashboard_uindex',
					'module' => 'default',
					'controller' => 'dashboard',
					'action' => 'uindex'
				),
				array(
					'label' => _('Sitemap'),
					'route' => 'seo_sitemap_index',
					'module' => 'seo',
					'controller' => 'sitemap',
					'action' => 'index'
				)		
			)
		);
		
		$view->admin_menu = $container;
	}
}
